package cn.com.yang.thread;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class GzipRunnable implements Runnable{
	private final File input;
	
	public GzipRunnable(File inpFile) {
		this.input =inpFile;
	}
	
	public void run() {
		//不压缩已经压缩的文件
		if (!input.getName().endsWith(".gz")) {
			File outFile = new File(input.getParent(),input.getName()+".gz");
			if (!outFile.exists()) {//不覆盖存在的文件
				try(InputStream in = new BufferedInputStream(new FileInputStream(input));
						OutputStream outputStream = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outFile)))
						){
						int b;
						while((b=in.read())!=-1)outputStream.write(b);
						outputStream.flush();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
}


